package com.reger.l2cache.bloomfilter.hash;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.reger.l2cache.bloomfilter.Hash;

/*
 * 实现一致性哈希算法中使用的哈希函数,使用MD5算法来保证一致性哈希的平衡性
 */
public class Md5Hash implements Hash {

	private static final MessageDigest md5;

	static {
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException("no md5 algrithm found");
		}
	}

	private final long cap;

	public Md5Hash(long cap) {
		super();
		this.cap = cap;
	}

	@Override
	public long hash(String key) {
		md5.reset();
		md5.update(key.getBytes());
		byte[] bKey = md5.digest();
		// 具体的哈希函数实现细节--每个字节 & 0xFF 再移位
		long result = ((long) (bKey[3] & 0xFF) << 24)
				| ((long) (bKey[2] & 0xFF) << 16 | ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF));
		return result & (cap - 1);
	}
}